<?php

namespace app\utils;

use app\utils\Hashids\Hashids as HashidsBase;
use think\Env;
use think\Config;

class Hashids
{
    private static $hashids;

    /**
     * 单列模型实例化
     * @param $salt
     * @param $hashLength
     * @return HashidsBase
     */
    public static function getInstanceHashids($salt, $hashLength)
    {
        if (!self::$hashids instanceof HashidsBase) {
            self::$hashids =  new HashidsBase($salt, $hashLength, Env::get('hashids.alphabet', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'));
        }
        return self::$hashids;
    }

    public static function encodeHex($str, $hashLength = 8)
    {
        $salt = Env::get('hashids.salt', Config::get('token.key'));

        $hashids = self::getInstanceHashids($salt, $hashLength);

        return $hashids->encodeHex($str);
    }

    public static function decodeHex($str, $hashLength = 8)
    {
        $salt = Env::get('hashids.salt', Config::get('token.key'));

        $hashids = self::getInstanceHashids($salt, $hashLength);

        return $hashids->decodeHex($str);
    }

}
